package com.tbit.main.service.impl;

import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.core.BmsBatteryDao;
import com.tbit.main.dao.log.PowerChangeLogExpandDao;
import com.tbit.main.pojo.BmsBattery;
import com.tbit.main.pojo.BmsStat;
import com.tbit.main.pojo.PowerChangeLogExpand;
import com.tbit.main.service.PowerChangeLogExpandService;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LMY
 * @create 2022-05-10 13:34
 * 换点记录拓展表
 */
@Service("powerChangeLogExpandService")
public class PowerChangeLogExpandServiceImpl implements PowerChangeLogExpandService {

    @Autowired
    private PowerChangeLogExpandDao powerChangeLogExpandDao;
    @Autowired
    private BmsBatteryDao bmsBatteryDao;


    @Override
    public void insert(PowerChangeLogExpand powerChangeLog) {
        powerChangeLogExpandDao.insert(powerChangeLog);
    }

    @Override
    public void update(PowerChangeLogExpand powerChangeLogExpand) {
        powerChangeLogExpandDao.update(powerChangeLogExpand);
    }

    @Override
    public PowerChangeLogExpand getPowerChangeId(Integer powerChangeId) {
        return powerChangeLogExpandDao.getPowerChangeId(powerChangeId);
    }

    @Override
    public List<PowerChangeLogExpand> getBatteryLog(Integer accountId, String startTime, String endTime, String batteryNO) {

        BmsStat bmsStat = new BmsStat();
        bmsStat.setBatteryNO(batteryNO);
        bmsStat.setAdAccountId(accountId);
        List<BmsBattery> bmsBatteries = bmsBatteryDao.getBattery(bmsStat);

        List<PowerChangeLogExpand> powerChangeLogExpands = new LinkedList<>();
        if (bmsBatteries != null && bmsBatteries.size() > 0) {
            List<String> batteryNOs = bmsBatteries.stream().map(BmsBattery::getBatteryNO).collect(Collectors.toList());
            Map<String, Object> map = new HashMap<>();
            map.put(DaoParamConstant.accountId, accountId);
            map.put(DaoParamConstant.startTime, startTime);
            map.put(DaoParamConstant.endTime, endTime);
            map.put("batteryNO", StringUtil.getStringListString(batteryNOs));
            powerChangeLogExpands = powerChangeLogExpandDao.getBatteryLog(map);
        }
        return powerChangeLogExpands;
    }

    @Override
    public PowerChangeLogExpand getUserLast(String batteryNO) {
        return powerChangeLogExpandDao.getUserLast(batteryNO);
    }

    @Override
    public List<PowerChangeLogExpand> getNO(String batteryNO) {
        return powerChangeLogExpandDao.getNO(batteryNO);
    }
}
